package com.amt.olcacon.controller;

import cn.hutool.json.JSONUtil;
import com.amt.olcacon.db.DerbyConfig;
import com.amt.olcacon.vo.GlobalResponse;
import com.amt.olcacon.vo.request.HelloRequest;
import com.amt.olcacon.vo.response.HelloResponse;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.openlca.core.DataDir;
import org.openlca.core.database.CategoryDao;
import org.openlca.core.database.FlowPropertyDao;
import org.openlca.core.model.Category;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: yys
 * @Date: 2025/5/27
 */
@Slf4j
@RestController
public class SayHelloController {

    @GetMapping("/sayHello")
    public String sayHello() {
        return "Hello World";
    }

    @GetMapping("/checkDerby")
    public String checkDerby() {
        var db = DataDir.get().openDatabase(DerbyConfig.DBNAME);
        log.info("db:{}", db);
        return JSONUtil.toJsonStr(new FlowPropertyDao(db).getForName("Mass").getFirst().name);
    }

    @GetMapping("/say/{name}")
    public String say(@PathVariable(value = "name") String name) {
        return "Hello " + name;
    }

    @PostMapping("sayHello")
    public GlobalResponse<HelloResponse> say(@RequestBody HelloRequest hello) {

        try (var db = DataDir.get().openDatabase(DerbyConfig.DBNAME)) {
            CategoryDao categoryDao = new CategoryDao(db);
//            Category category = new Category();
//            category.id = 10086;
//            category.name = "用户数据库";
//            category.description = "用户数据库";
//            categoryDao.insert(category);
//            log.info("category insert success! categoryId:{}", category.id);
//
            Category category = categoryDao.getForId(10086L);
            log.info("category:{}", new Gson().toJson(category));


        } catch (Exception e) {
            log.error("error", e);
        }

        HelloResponse response = new HelloResponse();
        response.setMessage(hello.getName() + hello.getCode() + hello.getAge());
        return GlobalResponse.of(response);
    }

}
